package com.qxzn.module.home.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.qxzn.app.api.api.HttpRequest;
import com.qxzn.app.bean.ContactBean;
import com.qxzn.app.page.adapter.ListBaseAdapter;
import com.qxzn.app.page.util.SuperViewHolder;
import com.qxzn.app.util.AppUtil;
import com.qxzn.app.util.SharePreferenceUtil;
import com.qxzn.module.home.R;
import com.qxzn.module.home.R2;

import java.util.Collections;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CareRemindAdapter extends ListBaseAdapter<ContactBean> {

    private LRecyclerViewAdapter mLRecyclerViewAdapter;
    private boolean dragEnabled;
    private ContactBean item;
    private ContactBean.UserresultBean detailBean;
    private View.OnClickListener msgClick;
    private View.OnClickListener remindClick;
    private String username;

    public CareRemindAdapter(Context context) {
        super(context);
        username = SharePreferenceUtil.getUserName(context);
    }

    @Override
    public int getLayoutId() {
        return R.layout.item_care_remind;
    }

    @Override
    public void onBindItemHolder(SuperViewHolder holder, int position) {
        AppUtil.showLog("onBindItemHolder");
        ImageView ivHead = holder.getView(R.id.iv_head);
        TextView tvName = holder.getView(R.id.tv_name);
        ImageView ivRemind = holder.getView(R.id.iv_remind);
        ImageView ivMessage = holder.getView(R.id.iv_message);
        ImageView ivDrag = holder.getView(R.id.iv_drag);
        item = mDataList.get(position);
        if (item != null) {
            detailBean = item.getUserresult();
            if (detailBean != null) {
                Glide.with(mContext).load(HttpRequest.BASE_URL + detailBean.getHeadportrait()).apply(RequestOptions.bitmapTransform(new CircleCrop()).placeholder(R.mipmap.logo).error(R.mipmap.logo)).into(ivHead);
                tvName.setText(detailBean.getNiname());
            }
        }
        if (dragEnabled) {
            ivDrag.setVisibility(View.VISIBLE);
            ivRemind.setVisibility(View.INVISIBLE);
            ivMessage.setVisibility(View.INVISIBLE);
        } else {
            ivDrag.setVisibility(View.INVISIBLE);
            ivRemind.setVisibility(View.VISIBLE);
            ivMessage.setVisibility(View.VISIBLE);
        }
        ivMessage.setTag(item);
        ivMessage.setOnClickListener(msgClick);
        ivRemind.setTag(item.getUsername());
        ivRemind.setOnClickListener(remindClick);

        if (TextUtils.equals(username, item.getUsername())) {
            ivMessage.setVisibility(View.INVISIBLE);
        } else {
            ivMessage.setVisibility(View.VISIBLE);
        }
    }

    public void onItemDragMoving(RecyclerView.ViewHolder source, RecyclerView.ViewHolder target) {
        int from = getViewHolderPosition(source);
        int to = getViewHolderPosition(target);
        AppUtil.showLog("onItemDragMoving from " + from + "   to  " + to);
        if (from < to) {
            for (int i = from; i < to; i++) {
                Collections.swap(getDataList(), i, i + 1);
            }
        } else {
            for (int i = from; i > to; i--) {
                Collections.swap(getDataList(), i, i - 1);
            }
        }
        mLRecyclerViewAdapter.notifyItemMoved(source.getAdapterPosition(), target.getAdapterPosition());
    }

    public int getViewHolderPosition(RecyclerView.ViewHolder viewHolder) {
        return viewHolder.getAdapterPosition() - (mLRecyclerViewAdapter.getHeaderViewsCount() + 1);
    }

    public void setLRecyclerViewAdapter(LRecyclerViewAdapter mLRecyclerViewAdapter) {
        this.mLRecyclerViewAdapter = mLRecyclerViewAdapter;
    }

    public void setDragEnabled(boolean dragEnabled) {
        this.dragEnabled = dragEnabled;
    }

    public void setMsgClick(View.OnClickListener msgClick) {
        this.msgClick = msgClick;
    }

    public void setRemindClick(View.OnClickListener remindClick) {
        this.remindClick = remindClick;
    }

    static class ViewHolder extends SuperViewHolder {
        @BindView(R2.id.iv_head)
        ImageView ivHead;
        @BindView(R2.id.iv_remind)
        ImageView ivRemind;
        @BindView(R2.id.iv_message)
        ImageView ivMessage;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}